package br.uem.din.ia;

import br.uem.din.ia.quebraCabeca.estruturaDeDados.No;
import br.uem.din.ia.quebraCabeca.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class FrameQuebraCabeca extends javax.swing.JFrame implements IInteracaoUsuario {

    private boolean passoAPasso;
    private boolean executarProximoPasso = false;
    private int custoDoCaminho = 0;
    private int QuantidadeDePassosDoCaminho = 0;

    public FrameQuebraCabeca(boolean passoAPasso) {
        initComponents();
        this.passoAPasso = passoAPasso;
    }

    @Override
    synchronized public boolean aguardarUsuario() {
        if (!passoAPasso) {
            return false;
        }

        if (executarProximoPasso) {
            executarProximoPasso = false;
            return false;
        }
        return true;
    }

    @Override
    public void imprimirEstado(No no) {
        jTextArea2.append("----------------\n");
        jTextArea2.append(no.getId() + ")\n");
        jTextArea2.append(no.toString());
        jTextArea2.append("\n\n");
        jTextArea2.setCaretPosition(jTextArea2.getText().length());
    }

    @Override
    public <T extends No> void desenharFronteira(Iterable<T> fronteira) {
        StringBuilder str = new StringBuilder("[");
        for (T no : fronteira) {
            str.append(" no(");
            str.append(no.getId());
            str.append(", [");
            str.append(resolverCaminho(no));
            str.append("], ");
            str.append(no.getCusto());
            str.append(")");
        }
        str.append("]");
        jTextArea1.append("\n");
        jTextArea1.append(str.toString());
        jTextArea1.setCaretPosition(jTextArea1.getText().length());
    }

    @Override
    public void imprimirSolucao(No no) {
        if (no == null) {
            new JOptionPane().showMessageDialog(rootPane, "Nenhuma solução foi encontrada!");
        } else {
            custoDoCaminho = 0;
            String solucao = "no (" + no.getId() + ", [" + resolverCaminho(no) + "])";
            txtSolucao.setText(solucao);
            txtInformacoes.setText("Custo do Caminho: " + custoDoCaminho + " | Quantidade de Passos: " + QuantidadeDePassosDoCaminho);
            new JOptionPane().showMessageDialog(rootPane, "Solução Encontrada:\n" + solucao);
        }
    }

    private String resolverCaminho(No no) {
        QuantidadeDePassosDoCaminho++;
        custoDoCaminho += no.getCusto();
        if (no.getCaminho() == null) {
            return String.valueOf(no.getId());
        }

        return resolverCaminho(no.getCaminho()) + "|" + no.getId();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btAvanceUmPasso = new javax.swing.JButton();
        btExecuteAteFim = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtSolucao = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        btPause = new javax.swing.JButton();
        txtInformacoes = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        btAvanceUmPasso.setText("Avançe Um Passo >");
        btAvanceUmPasso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAvanceUmPassoActionPerformed(evt);
            }
        });

        btExecuteAteFim.setText("Execute Até o Fim >>");
        btExecuteAteFim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExecuteAteFimActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jScrollPane2.setViewportView(jTextArea2);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Fronteira");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Estados");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel3.setText("Solução");

        btPause.setText("Pause ||");
        btPause.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPauseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btAvanceUmPasso, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btExecuteAteFim, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btPause, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                        .addGap(240, 240, 240))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 515, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 116, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(txtSolucao, javax.swing.GroupLayout.DEFAULT_SIZE, 699, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtInformacoes, javax.swing.GroupLayout.DEFAULT_SIZE, 699, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSolucao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(txtInformacoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btAvanceUmPasso, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
                    .addComponent(btExecuteAteFim, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
                    .addComponent(btPause, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE))
                .addContainerGap())
        );

        getAccessibleContext().setAccessibleParent(this);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
}//GEN-LAST:event_formWindowOpened

private void btAvanceUmPassoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAvanceUmPassoActionPerformed
    executarProximoPasso = true;
}//GEN-LAST:event_btAvanceUmPassoActionPerformed

private void btExecuteAteFimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExecuteAteFimActionPerformed
    passoAPasso = false;
}//GEN-LAST:event_btExecuteAteFimActionPerformed

private void btPauseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPauseActionPerformed
    passoAPasso = true;
    executarProximoPasso = false;
}//GEN-LAST:event_btPauseActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAvanceUmPasso;
    private javax.swing.JButton btExecuteAteFim;
    private javax.swing.JButton btPause;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextField txtInformacoes;
    private javax.swing.JTextField txtSolucao;
    // End of variables declaration//GEN-END:variables
}
